<?php
/**
 * (actions)
 */
namespace App\Admin\Actions\ProductStockup;

use Encore\Admin\Grid\Tools\AbstractTool;
class CheckboxBatchConfirm extends AbstractTool
{
    /**
     * Render Export button.
     *
     * @return string
     */
    public function render()
    {

        return <<<EOT
        <div class="btn-group pull-left" style="margin-right: 10px; line-height: 30px;"><span>全选</span> <input type="checkbox" class="grid-select-all" /></div>
        <div class="btn-group pull-left" style="margin-right: 10px;">

            <a class="btn btn-sm btn-default">操作</a>
            <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
                <span class="caret"></span>
                <span class="sr-only">Toggle Dropdown</span>
            </button>
            <ul class="dropdown-menu" role="menu">
                <li><a href="#" class="grid-batch-1">确认</a></li>
            </ul>
        </div>

        <script language="JavaScript" type="text/javascript">
        $(function () {

            //列展开 (默认添加模板数据)
            $('.grid-expand-grid-row').each(function() {
                let name = $(this).data('name');
                let row = $(this).closest('tr');
                row.after($('template.grid-expand-'+name).html());
            });

            //列展开
            $('.grid-expand-grid-row').on('click', function () {
                if ($(this).data('inserted') == '0') {
                    $(this).data('inserted', 1);
                }
                $("i", this).toggleClass("fa-angle-double-down fa-angle-double-up");
            });

            //确认
            $('.grid-batch-1').on('click', function() {

                let selectRows = [];
                $("input[type=checkbox]:checked").each(function() {

                    let _this = $(this);
                    let classify_id = _this.data('classify-id');
                    let area_id = _this.data('area-id');
                    let psku = [];
                    $("input[name=this_psku_id_"+ classify_id + area_id +"]").each(function() {
                        let psku_id = $(this).val();
                        let stockup_id =  $.trim($('#this_stockup_id_' + classify_id + area_id + psku_id).val());
                        let num = $.trim($('#this_num_' + classify_id + area_id + psku_id).text());
                        let remark = $.trim($('#this_remark_' + classify_id + area_id + psku_id).text());
                        let data = {
                            stockup_id: stockup_id,
                            classify_id : classify_id,
                            area_id : area_id,
                            psku_id: psku_id,
                            num : num,
                            remark : remark,
                        };
                        psku.push(data);
                    });
                    selectRows.push(psku);
                });

                if (selectRows.length == 0) {
                    swal('请选择备货数据', '', 'error');
                    return false;
                }

                $.ajax({
                    method: 'post',
                    url: '/xqc/product-stockup/stockup-ajax',
                    data: {
                        _token: "{{ csrf_token() }}",
                        action: 'stockupApplyBatchConfirm',
                        param: selectRows,
                    },
                    success: function (data) {
                        $.pjax.reload('#pjax-container');
                        toastr.success(data.message);
                    },
                    error: function (data) {
                        swal(data.message, '', 'error');
                    }
                });
            });

        });
    </script>

EOT;
    }


}
